package com.wang.fastdfs.utils;

        import org.csource.common.MyException;
        import org.csource.fastdfs.*;

        import java.io.FileOutputStream;
        import java.io.IOException;

/**
 * @author: wang
 * @date: 2021/7/29 20:22
 * @description:
 */
public class FastdfsUtil {

    public static void main(String[] args) throws IOException, MyException {
        /*String path = "C:\\Users\\Administrator\\Pictures\\Saved Pictures\\收藏壁纸\\动漫\\忘了名字了.jpg";
        String suffix = path.substring(path.lastIndexOf(".")+1);
        fileUpload(path,suffix);*/

        // fileDownload("group1", "M00/00/00/wKipe2EDGwqASWMXAAr_APCj1nY161.jpg", "D:\\123\\123.jpg");
        //fileDelete("group1", "M00/00/00/wKipe2EDGwqASWMXAAr_APCj1nY161.jpg");

    }
    // 上传文件的方法
    public static String fileUpload(byte[] bytes, String suffix){
        StorageClient storageClient = Client.CLIENT.getClient();
        //6.上传文件  第一个参数：本地文件路径 第二个参数：上传文件的后缀 第三个参数：文件信息 D:/aa.txt txt
        String [] uploadArray = new String[0];
        try {
            uploadArray = storageClient.upload_file(bytes,suffix, null);
        } catch (IOException | MyException e) {
            e.printStackTrace();
        }
        return "http://192.168.169.123/"+uploadArray[0]+"/"+uploadArray[1];
    }

    // 下载文件的方法
    public static void fileDownload(String group_name, String remote_file_name, String local_file_name){
        StorageClient storageClient = Client.CLIENT.getClient();
        int i = 0;
        try {
            i = storageClient.download_file(group_name, remote_file_name, local_file_name);
        } catch (IOException | MyException e) {
            e.printStackTrace();
        }
        if (i!=0){
                System.out.println("下载失败");
            }
    }

    // 删除文件的方法
    public static void fileDelete(String group_name, String remote_file_name) {
        StorageClient storageClient = Client.CLIENT.getClient();
        int i = 0;
        try {
            i = storageClient.delete_file(group_name, remote_file_name);
        } catch (IOException | MyException e) {
            e.printStackTrace();
        }
        if (i!=0)
            System.out.println("删除失败");
    }

    /*// 获取客户端的方法
    public static StorageClient getClient() throws IOException, MyException {
        //1.加载配置文件，默认去classpath下加载
        ClientGlobal.init("fdfs_client.conf");
        //2.创建TrackerClient对象
        TrackerClient trackerClient = new TrackerClient();
        //3.创建TrackerServer对象
        TrackerServer trackerServer = trackerClient.getConnection();
        //4.创建StorageServler对象
        StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
        //5.创建StorageClient对象，这个对象完成对文件的操作
        return new StorageClient(trackerServer,storageServer);
    }*/

    /*// 关闭客户端的方法
    public static void closeClient(StorageServer storageServer, TrackerServer trackerServer){
        if(storageServer != null){
            try {
                storageServer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(trackerServer != null){
            try {
                trackerServer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }*/
}

enum Client{
    CLIENT;

    private StorageClient storageClient;
    private TrackerServer trackerServer;
    private StorageServer storageServer;

            Client(){
        try {
            //1.加载配置文件，默认去classpath下加载
            ClientGlobal.init("fdfs_client.conf");
            //2.创建TrackerClient对象
            TrackerClient trackerClient = new TrackerClient();
            //3.创建TrackerServer对象
            trackerServer = trackerClient.getConnection();
            //4.创建StorageServler对象
            storageServer = trackerClient.getStoreStorage(trackerServer);
            //5.创建StorageClient对象，这个对象完成对文件的操作
            storageClient = new StorageClient(trackerServer,storageServer);
        } catch (IOException | MyException e) {
            e.printStackTrace();
        }
    }

    // 获取客户端的方法
    public StorageClient getClient(){
        return storageClient;
    }
    // 关闭客户端的方法
    public void closeClient(){
        if(storageServer != null){
            try {
                storageServer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(trackerServer != null){
            try {
                trackerServer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


